const call = require('../../utils/request.js');
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    doctorId: null, //医生ID
    groupId: null, //问题ID（追加）
    content: '', //内容
    imgArr: [], //图片的路径数组
    imgArr64: [], //base64位
    otherArr: [] //补充
  },

  onLoad: function (options) {
    this.setData({
      doctorId:options.id || null,
      groupId:options.groupId || null
    })
  },

  onReady: function () {

  },

  onShow: function () {

  },

  onHide: function () {
    
  },

  //选择图片
  chooseImg(e) {
    wx.chooseImage({
      count: (3 - this.data.imgArr.length), 
      sizeType: ['copressed'], 
      success: (res)=> {
        var arr = this.data.imgArr, arr64 = this.data.imgArr64; 
        // for(let i of res.tempFilePaths){
        //   _default.$set(this.imgArr,this.imgArr.length,i)
        // }
        arr = arr.concat(res.tempFilePaths); 
        for (var i in res.tempFiles) {
          wx.getFileSystemManager().readFile({
            filePath: res.tempFiles[i].path, encoding: 'base64', success: function success(r) {
              var base64 = 'data:image/jpeg;base64,' + r.data;
              arr64.push(base64);
            }
          });
        }
        var arr1 = [];
        for (var _i = 0, len = 3 % (arr.length % 3 + 1); _i < len; _i++) {
          arr1.push('');
        }

        this.setData({
          imgArr:arr,
          otherArr:arr1,
        })
      }
    });

  },

  //删除图片
  delImg(e) {
    const index = e.currentTarget.dataset.index;
    var arr = this.imgArr,arr64 = this.imgArr64;
    arr.splice(index, 1);
    arr64.splice(index,1);

    var arr1 = [];
    for (var i = 0, len = 3 % (arr.length % 3 + 1); i < len; i++) {
      arr1.push('');
    }
    this.setData({
      imgArr:arr,
      imgArr64:arr64,
      otherArr:arr1
    })
  },

  // 提交,首次跳转到支付页面，追加则跳转到问答页面
  toUrl(e) {
    if (this.data.content.trim().length > 0) {
      if (this.data.groupId) {//提问并跳转到问答页面
        this.uploadImg();
      } else {//跳转到支付页面
        var obj = {
          id: this.data.doctorId,
          content: this.data.content,
          imgArr: this.data.imgArr64
        };

        wx.setStorageSync('question', JSON.stringify(obj));
        wx.redirectTo({
          url: '../buy/buy?type=2'
        });

      }
    } else {
      wx.showToast({
        title: '请输入您的问题',
        icon: 'none'
      });

    }
  },

  // 上传图片
  uploadImg() {
    if (this.data.imgArr64.length > 0) {
      wx.showLoading({
        title: '正在上传图片',
        mask: true
      })
      call.post('/api/upload/base64_img', { file: this.data.imgArr64.join('|') }).then( (res)=> {
        wx.hideLoading();
        console.log("上传图片", res.data.data);
        if (res.statusCode == 200 && res.data.code == 0) {
          this.sendQues(res.data.data);
        }
      });
    } else {
      this.sendQues('');
    }

  },

  //提交问题
  sendQues(img) {
    var data = {
      group_id: this.data.groupId,
      content: this.data.content,
      image_source: img
    };

    wx.showLoading({
      title: '正在提交数据',
      mask: true
    })
    call.post('/api/graphic/additional', data).then( (res1)=> {
      wx.hideLoading();
      console.log("发起提问", res1);
      if (res1.statusCode == 200 && res1.data.code == 0) {
        // wx.redirectTo({
        //   url: "../myQuestion/myQuestion?id=".concat(_this3.groupId)
        // });
        
        let pages = getCurrentPages();
        let beforePage = pages[pages.length - 2];
        wx.navigateBack({
          delta: 1,
          success:()=>{
            if(this.data.groupId){
              console.log(beforePage)
              beforePage.getQues();
            }
          }
        })
      }
    });
  },

  // 文本框的数据绑定
  inputChange(e) {
    this.setData({
      content:e.detail.value
    })
  },
})